package com.hydra.base.utils;

import com.hydra.base.error.SystemException;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 泛型工具类
 * @Author Hydra
 * @Date 2022/5/1 23:30
 */
public class ParameterizedTypeUtil {

    /**
     * 从类对象中获取泛型
     */
    public static Type getFromClass(Class<?> clazz){
        Type genericSuperclass = clazz.getGenericSuperclass();
        if (!(genericSuperclass instanceof ParameterizedType)) {
            throw new SystemException("接口获取泛型定义异常");
        }
        Type[] arguments = ((ParameterizedType) genericSuperclass).getActualTypeArguments();
        if(arguments.length == 0){
            throw new SystemException("接口获取实体泛型异常");
        }
        return arguments[0];
    }

}
